//分页数据
  var totalPage =2;//一共多少页数据
  var pageSize =6;//每页显示10条数据
  var currentPage =1;//当前第几页数据，默认为1
  var isLoading = false;//是否正在加载更多数据//
$(function(){
	//重置
	$('#btn-reset').click(function(){
		$('#search-hallName').val("");
	})
	//查询所有影厅信息
	
	queryHall(1);
	function queryHall(pageNum){
		var p={};
		p.pageNum=pageNum;
		p.pageSize=pageSize;
		$('#user-order').empty();
		  $('#user-order').append('<tr><td></td><td  hidden="true">主键号</td> <td>序号</td> <td>影厅名称</td><td>影厅别名</td><td>座位信息</td></tr>');
	$.ajax({
		  type:"POST",
		  url:"/movie.front/hallConntroller/findAllHall",
		  data:JSON.stringify(p),
		  dataType:"json",
		  contentType:"application/json",
		  success:function(data){
           array = data.result.list;
           var totalPages=0;
           if(array.length>1){
           for(var i=0;i<array.length;i++){
             var html = '' ;
             if(array[i].newPages==""||array[i].newPages==undefined){
              html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+(i+1)+'</td><td>'+array[i].hallName+'</td><td>'+array[i].hallAlias+'</td><td>'+(array[i].seatsx+1)+'排'+(array[i].seatsy+1)+'座</td></tr>';
           	  $('#user-order').append(html); 
           }else{
        	   totalPages = array[i].newPages;
           }
           }
          }
           $('#paginator').empty();
        	   var pageHtml = '<ul>';
        	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="firstPage" onclick="firstPagesquery(1)"> 首页 </a></li>';
        	   for(var i=0;i<totalPages;i++){
        		  pageHtml = pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" onclick="currentPagesquery('+(i+1)+')" id='+(i+1)+'>'+(i+1)+'</a></li>'; 
        	   }
        	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="endPage" onclick="endPagequery('+totalPages+')">尾页</a></li>';
        	   pageHtml =pageHtml+'</ul>';
        	   $('#paginator').append(pageHtml);
          }
	
	   });
		
	}
	//查询信息
	$('#btn-search').click(function(){
    var hallName =	$('#search-hallName')[0].value;
	
    queryHallSearch(1,hallName);
	});
	
	function queryHallSearch(pageNum,hallName){
		var p={};
		p.pageNum=pageNum;
		p.pageSize=pageSize;
		p.hallName = hallName;
		$('#user-order').empty();
		  $('#user-order').append('<tr><td></td><td  hidden="true">主键号</td> <td>序号</td> <td>影厅名称</td><td>影厅别名</td><td>座位信息</td></tr>');
	$.ajax({
		  type:"POST",
		  url:"/movie.front/hallConntroller/findAllHall",
		  data:JSON.stringify(p),
		  dataType:"json",
		  contentType:"application/json",
		  success:function(data){
           array = data.result.list;
           var totalPages=0;
           for(var i=0;i<array.length;i++){
             var html = '' ;
             if(array[i].newPages==""||array[i].newPages==undefined){
              html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+(i+1)+'</td><td>'+array[i].hallName+'</td><td>'+array[i].hallAlias+'</td><td>'+(array[i].seatsx+1)+'排'+(array[i].seatsy+1)+'座</td></tr>';
           	  $('#user-order').append(html); 
           }else{
        	   totalPages = array[i].newPages;
           }
           }
           $('#paginator').empty();
        	   var pageHtml = '<ul>';
        	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="firstPage" onclick="firstPagesquery(1)"> 首页 </a></li>';
        	   for(var i=0;i<totalPages;i++){
        		  pageHtml = pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" onclick="currentPagesquery('+(i+1)+')" id='+(i+1)+'>'+(i+1)+'</a></li>'; 
        	   }
        	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="endPage" onclick="endPagequery('+totalPages+')">尾页</a></li>';
        	   pageHtml =pageHtml+'</ul>';
        	   $('#paginator').append(pageHtml);
          }
	
	   });
		
		
	}
	
	$('#btn-insert').click(function(){
		  $('#id')[0].value="";
		$('#hallName').val("");
		$('#hallAlias').val("");
		$('#seatNumberX')[0].value="";
		$('#seatNumberY')[0].value="";
		$('#myModal').modal('show');
		/*alert('aaaaaaa');*/
	});
	//保存用户信息
	$('#btn-save-hall').click(function(){
	var hallName =	$('#hallName')[0].value;
	var hallAlias=$('#hallAlias')[0].value;
	var seatNumberY=$('#seatNumberY')[0].value;
	var seatNumberX=$('#seatNumberX')[0].value;
	var id = $('#id')[0].value;
	if(id==null || id==""){
		var p={id:"",hallName:hallName,hallAlias:hallAlias,seatNumberY:seatNumberY,seatNumberX:seatNumberX};
	$.ajax({
		  type:"POST",
		  url:"/movie.front/hallConntroller/addHall",
		  data:JSON.stringify(p),
		  dataType:"json",
		  contentType:"application/json",
		  success:function(data){
            if(data.successCode!=null){
            	//关闭对话框
            	$('#myModal').modal('hide');
            	//刷新表格
            		queryHall(1);
            }
            if(data.errorcode!=null){
            	alert(data.errorcode);
            }
		   
		  }
	
	   });
	}else{
		var p={id:id,hallName:hallName,hallAlias:hallAlias,seatNumberY:seatNumberY,seatNumberX:seatNumberX};
		$.ajax({
			  type:"POST",
			  url:"/movie.front/hallConntroller/updateHall",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
				  if(data.successCode!=null){
					  $('#myModal').modal('hide');
						//刷新表格
					  queryHall(1);
			            					  
				  }
				  if(data.errorcode!=null){
		            	alert(data.errorcode);
		            }

			   
			  }
		
		   });
	 }
		
	});
	
	//修改用户信息
	$('#btn-update').click(function(){
		var flag=0;
		var id =0;
		var array = $(("input[type='checkbox']"));
		for(var i=0;i<array.length;i++){
		  console.info(array[i].id);
		  if($('#'+array[i].id).is(':checked')){
			var id = array[i].id;
		      flag++;
		      if(flag>1){
		    	  alert("对不起，只能选择单行");
		    	return false;
		      }else{
		    	  id=array[i].id;
		      }
		
		  }
	   
		}
		  if(flag==1){
			  $('#myModal').modal('show');
			  var id = {id:id};
			  getByIdUserInfo(id);
		     }
	});
	//影厅座位重置
	$('#btn-reset-seats').click(function(){
		var flag=0;
		var id=0;
		var array = $(("input[type='checkbox']"));
		for(var i=0;i<array.length;i++){
		  console.info(array[i].id);
		  if($('#'+array[i].id).is(':checked')){
		      flag++;
		      if(flag>1){
		    	  alert("对不起，只能选择单行");
		    	 return false;
		      }else{
		    	  var id = array[i].id;
		      }
		
		  }
	   
		}
		  if(flag==1){
		    	resetSeats(id);
		      }
	});
	
	function resetSeats(id){
		var p ={};
		    p.id = id;
		$.ajax({
			  type:"POST",
			  url:"/movie.front/hallConntroller/updateSeatsByHallId",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
				  if(data.successCode!=null){
					  alert("重置成功");
						//刷新表格
					  queryHall(1);				  
				  }
				  if(data.errorcode!=null){
		            	alert(data.errorcode);
		            }
				 
				 
				  
			     }
		   });
		
	}
	//删除用户信息
	$('#btn-remove').click(function(){
		var flag=0;
		var id=0;
		var array = $(("input[type='checkbox']"));
		for(var i=0;i<array.length;i++){
		  console.info(array[i].id);
		  if($('#'+array[i].id).is(':checked')){
		      flag++;
		      if(flag>1){
		    	  alert("对不起，只能选择单行");
		    	 return false;
		      }else{
		    	  var id = array[i].id;
		      }
		
		  }
	   
		}
		  if(flag==1){
		    	 var id = {id:id};
		    	 deleteUserInfo(id);
		      }
		
	});
	function deleteUserInfo(p){
		$.ajax({
			  type:"POST",
			  url:"/movie.front/hallConntroller/deleteHall",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
				  if(data.successCode!=null){
					  alert("删除成功");
						//刷新表格
					  queryHall(1);				  
				  }
				  if(data.errorcode!=null){
		            	alert(data.errorcode);
		            }
				 
				 
				  
			     }
		   });
	}
	function getByIdUserInfo(p){
		//回显信息
	
		$.ajax({
			  type:"POST",
			  url:"/movie.front/hallConntroller/getHallById",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
	           $('#hallName')[0].value=data.result.hallName;
	           $('#hallAlias')[0].value=data.result.hallAlias;
	           $('#id')[0].value=data.result.id;
	           $('#seatNumberX')[0].value=data.result.seatNumberX+1;
	           $('#seatNumberY')[0].value=data.result.seatNumberY+1;
			  }
		
		   });
	}
	
	
 });

//当前页
function currentPagesquery(currentId){
	 // alert(currentId);当前页码
	  $('#paginator').empty();
	     var p ={};
         p.pageSize = 6;
         p.pageNum = currentId;
         $('#user-order').empty();
		  $('#user-order').append('<tr><td></td><td  hidden="true">主键号</td> <td>序号</td> <td>影厅名称</td><td>影厅别名</td><td>座位信息</td></tr>');
	$.ajax({
		  type:"POST",
		  url:"/movie.front/hallConntroller/findAllHall",
		  data:JSON.stringify(p),
		  dataType:"json",
		  contentType:"application/json",
		  success:function(data){
          array = data.result.list;
          var totalPages=0;
         if(array.length>1){
          for(var i=0;i<array.length;i++){
            var html = '' ;
            if(array[i].newPages==""||array[i].newPages==undefined){
             html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+(i+1)+'</td><td>'+array[i].hallName+'</td><td>'+array[i].hallAlias+'</td><td>'+(array[i].seatsx+1)+'排'+(array[i].seatsy+1)+'座</td></tr>';
          	  $('#user-order').append(html); 
          }else{
       	   totalPages = array[i].newPages;
          }
          }
		 }
          $('#paginator').empty();
       	   var pageHtml = '<ul>';
       	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="firstPage" onclick="firstPagesquery(1)"> 首页 </a></li>';
       	   for(var i=0;i<totalPages;i++){
       		  pageHtml = pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" onclick="currentPagesquery('+(i+1)+')" id='+(i+1)+'>'+(i+1)+'</a></li>'; 
       	   }
       	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="endPage" onclick="endPagequery('+totalPages+')">尾页</a></li>';
       	   pageHtml =pageHtml+'</ul>';
       	   $('#paginator').append(pageHtml);
         }
	
	   });
		
}


//首页
function  firstPagesquery(firstPageId){
	currentPagesquery(firstPageId);
}

//尾页
function  endPagequery(endPageId){
	currentPagesquery(endPageId);
	 }
